package com.example.nonacademic_education.util.timer;

import com.example.nonacademic_education.constant.Constant;
import org.apache.commons.io.FileUtils;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.io.File;

/**
 * @author xinmiao
 * @description
 * @date 2023/3/26 17:01
 */
public class DelPdfFile implements Job {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        File directory = new File(Constant.proBiologyImagePath+Constant.PDF_PATH);
        if (directory.isDirectory()) {
            File[] files = directory.listFiles();
            if (files == null || files.length == 0) {
                System.out.println("Directory is empty.");
            } else {
                System.out.println("Directory is not empty.");
                //delete files
                try {
                    FileUtils.cleanDirectory(directory); // Apache Commons IO 的 cleanDirectory() 方法
                    //使用deleteRecursively 可以删除包括文件夹在内的所有文件
                    //org.apache.commons.io.FileUtils.deleteRecursively(directory); // deleteRecursively() 方法
                    System.out.println("Deleted all files and subdirectories in: " + directory.getAbsolutePath());
                } catch (Exception e) {
                    System.out.println("Failed to delete files in: " + directory.getAbsolutePath());
                }
            }
        } else {
            System.out.println("Not a directory.");
        }
    }
}
